
var treeDialoguxTree = new Ext.tree.TreePanel({
			autoScroll : true,
			height : 600,
			animate : false,
			useArrows : true,
			autoDestroy : true,
			dataUrl :basepath+'/relation.do?action=relationDataList',
			root : {
				nodeType : 'async',
				id : '0'
			},
			tbar : [{
						xtype : 'combo',
						width : 60,
						displayField : 'field1',
						valueField : 'field2',
						hiddenName : 'field2',
						editable : false,
						mode : "local",
						value : "name",
						store : new Ext.data.SimpleStore({
									fields : ['field1', 'field2'],
									data : [['名称', 'name'], ['代码', 'code']]
								}),
						mode : 'local',
						triggerAction : 'all'
					}, {
						xtype : 'textfield',
						name : 'fdcName',
						emptyText : '请输入过滤条件...'

					}]
		});
var treeDialogWin = new Ext.Window({
			layout : 'fit',
			width : 360,
			height : 490,
			modal : true,
			closeAction : 'hide',
			plain : true,
			items : [treeDialoguxTree]
		});

Ext.ux.TreeDialog = Ext.extend(Ext.form.TriggerField, {
	editable : true,
	triggerClass : 'x-form-search-trigger',
	selectLeaf : true,// 是否只允许选择叶子
	elementcode : '',// 主表elementcode
	byElementcode : '',// 被关联表elementcode
	mainId : '',// 默认主表代码
	treeClikFunction : null,// 当点选tree节点时出发方法
	findMainIdFunction : null,// 获得主键code的方法 如果方法的返回值为false则不打开窗口
	dataParams : null,// dataParams:[{"name":"字段名","value":"数据值"}]
	winTitle : null,
	rootText : null,
	treeurl : null,//'../relation.do?action=relationDataList',
	hiddenName : null,
	isCodeSelect : false,
	changeTextValue:null,// changeTextValue:[{"name":"字段名","value":"数据值"}]
    codeUrl:'../relation.do?action=relationCodeData',
	onClickFunction : function(node) {
          if(node.attributes.id=='0')
	        return;
		if (!this.selectLeaf || node.leaf) {
			this.setValue(node.text);
			this.code = node.attributes.code;
			this.mydataid = node.attributes.id;
			if (this.hiddenField)
				this.hiddenField.value = this.mydataid;
		if (this.treeClikFunction)
			this.treeClikFunction(node);
			treeDialogWin.hide();

		}

	},
	treeNodeDestroy : function(node) {
		if (!node)
			return;

		node.collapse(false, false);
		while (node.firstChild) {
			node.removeChild(node.firstChild);
		}
		node.childrenRendered = false;
		node.loaded = false;
	},
	onWinHide : function() {
		treeDialoguxTree.un("click", this.onClickFunction, this);
		treeDialogWin.un("hide", this.onWinHide, this);

		var loader = treeDialoguxTree.getLoader();

		if (this.dataParams) {

			for (var i = 0; i < this.dataParams.length; i++) {

				delete loader.baseParams[this.dataParams[i].name];
			}

		}

		this.treeNodeDestroy(treeDialoguxTree.getRootNode());

	},
	onTriggerClick : function() {
		this.hiddenPkgs = [];
		var tmpMainId = this.findMainIdFunction
				? this.findMainIdFunction(this)
				: null;
		if (tmpMainId === false)
			return;
		treeDialogWin.on("hide", this.onWinHide, this);
		var myfieldLabel = this.fieldLabel ? this.fieldLabel : '';
		treeDialogWin.setTitle(this.winTitle ? this.winTitle : myfieldLabel);
		treeDialoguxTree.getRootNode().setText(this.rootText
				? this.rootText
				: myfieldLabel);
		treeDialoguxTree.topToolbar.get(0).setValue('name');
		treeDialoguxTree.topToolbar.get(1).setValue('');

		var loader = treeDialoguxTree.getLoader();
        if(this.treeurl)
        loader.dataUrl=this.treeurl;
		if (this.dataParams) {

			for (var i = 0; i < this.dataParams.length; i++) {

				loader.baseParams[this.dataParams[i].name] = this.dataParams[i].value;
			}

		}

		loader.baseParams['elementcode'] = this.elementcode;
		loader.baseParams['byElementcode'] = this.byElementcode;
		loader.baseParams['mainId'] = tmpMainId ? tmpMainId : this.mainId;
		
		treeDialoguxTree.root.reload(function(node) {
					if (this.childNodes.length <= 0) {
						Ext.MessageBox.show({
									title : '没有可选择项',
									width : 160,
									msg : "没有可选择项",
									buttons : Ext.Msg.OK,
									icon : Ext.MessageBox.INFO,
									fn:function(){treeDialogWin.hide()}
								});
								
							
					}
					

				});
		treeDialogWin.show(this);
		treeDialoguxTree.on("click", this.onClickFunction, this);

	},
	getValue : function() {
		if ((this.mydataid || this.mydataid == '') && this.hiddenField)
			this.mydataid = this.hiddenField.value;
		return this.mydataid;
	},
	getCode : function() {
		return this.code;
	},
	getText : function() {
		return this.text;
	},
	onRender : function(ct, position) {

		Ext.ux.TreeDialog.superclass.onRender.call(this, ct, position);
		if (this.hiddenName) {
			function getOwnerCt(ownerCt) {
				if (!ownerCt)
					return null;
				if (ownerCt.bodyCfg && ownerCt.bodyCfg.tag == 'form') {
					return ownerCt;
				} else {
					return getOwnerCt(ownerCt.ownerCt);
				}

			}

			this.hiddenField = this.el.insertSibling({
						tag : 'input',
						type : 'hidden',
						name : this.hiddenName,
						id : (this.hiddenId || this.hiddenName)
					}, 'before', true);
			var formOwnerCt = getOwnerCt(this.ownerCt);
			if (formOwnerCt&&!formOwnerCt.getForm().hiddenNames) {

				var tmphiddenName = [];

				tmphiddenName[this.hiddenName] = this.name;
				formOwnerCt.getForm().hiddenNames = tmphiddenName;
				formOwnerCt.getForm().setValues = function(values) {
					if (Ext.isArray(values)) { // array of objects
						for (var i = 0, len = values.length; i < len; i++) {
							var v = values[i];
							var f = this.findField(v.id);
							if (f) {
								f.setValue(v.value);
								if (this.trackResetOnLoad) {
									f.originalValue = f.getValue();
								}
							} else {
								if (this.hiddenNames[v.id]) {
									var f4 = this.findField(this.hiddenNames[v.id]);
									f4.mydataid = v.value;
									f4.hiddenField.value = v.value;
								}
							}
						}
					} else { // object hash
						var field, id;
						for (id in values) {
							if (!Ext.isFunction(values[id])
									&& (field = this.findField(id))) {
								field.setValue(values[id]);
								if (this.trackResetOnLoad) {
									field.originalValue = field.getValue();
								}
							} else {
								if (this.hiddenNames[id]) {
									var f4 = this.findField(this.hiddenNames[id]);
									
									f4.mydataid = values[id];
									f4.hiddenField.value = values[id];

								}
							}
						}
					}
					return this;

				}
			} else {
				var formOwnerCt = getOwnerCt(this.ownerCt);
				if(formOwnerCt)
				formOwnerCt.getForm().hiddenNames[this.hiddenName] = this.name;

			}
		}
	},
	initComponent : function() {
		this.hiddenPkgs = [];
		this.mydataid = null;
		this.code = '';
		this.text = '';

	},
	onBlur : function() {
		var thisobj = this;
		///////////////////20100708 添加用于清空关联项/
		var changeTextValue=this.changeTextValue;
		var ownerCts=this.ownerCt;
		///////////////////////////////////////
		if (this.isCodeSelect) {
			var v = this.el.getValue();
			if (v != '') {
				var sSend = 'code=' + v + '&byElementcode='+ this.byElementcode+"&selectLeaf="+this.selectLeaf;
				Ext.lib.Ajax.request('POST',this.codeUrl, {
							success : function(request) {
								var message = request.responseText;
								if (message != '') {
            
									var o = Ext.util.JSON.decode(message);
									thisobj.el.dom.value = o.name;
									thisobj.text = o.name;
									thisobj.code = o.code;
									thisobj.mydataid = o.id;
									if (thisobj.hiddenField)
										thisobj.hiddenField.value = o.id;
										
										
										
								   //20100708 添加用于清空关联项///////////////////////	
								   if(changeTextValue){
								    	function getOwnerCt1(ownerCt) {
											if (!ownerCt)
												return null;
											if (ownerCt.bodyCfg && ownerCt.bodyCfg.tag == 'form') {
												return ownerCt;
											} else {
												return getOwnerCt1(ownerCt.ownerCt);
											}
										}
									    var formOwnerCt1 = getOwnerCt1(ownerCts);
									    for (var i = 0; i < changeTextValue.length; i++) {
											formOwnerCt1.getForm().findField(changeTextValue[i].name).setValue(changeTextValue[i].value);
										}
								    }	
								    ///////////////////////////////////////////////////
								} else {
									if (thisobj.nullclear()) {
										Ext.Msg.alert("提示", "未找到"
														+ thisobj.fieldLabel
														+ "对应" + v + "代码的项");
									}
								}
							},
							failure : function() {
								Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
							}
						}, sSend);
		    }
		} else {
			this.nullclear();
		}
	},
	setValue : function(v) {
		this.text = v;
		Ext.ux.TreeDialog.superclass.setValue.call(this, v);
	},
	nullclear : function() {
		var v = this.el.getValue();

		if (v != this.text || !this.text || this.text == '') {
			this.el.dom.value = '';
			this.text = '';
			this.code = '';
			this.mydataid = '';
			if (this.hiddenField)
				this.hiddenField.value = '';
			return true;
		}
		return false;
	},
	onDestroy : function() {
		// if(treeDialogWin){
		// Ext.destroy(treeDialoguxTree, treeDialogWin);
		// }
		treeDialoguxTree.un("click", this.onClickFunction, this);
		treeDialoguxTree.getSelectionModel()
		treeDialogWin.un("hide", this.onWinHide, this);
		Ext.destroyMembers(this, 'hiddenField');
		Ext.ux.TreeDialog.superclass.onDestroy.call(this);
		// treeDialoguxTree=null;
		// treeDialogWin=null;

	}
});

Ext.reg('treeDialog', Ext.ux.TreeDialog);
